package com.gogowan.agroenterprise.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import com.gogowan.agroenterprise.contorl.Base64;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore.Video.Thumbnails;

/**
 * 图片工具类
 * 
 * @author zk
 * 
 */
public class IconUtils {

	/**
	 * 字节数组转换成Bitmap
	 * 
	 * @param b
	 * @return
	 */
	public static Bitmap bytes2Bitmap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		return null;
	}

	/**
	 * Bitmap转换成字节数组
	 * 
	 * @param bitmap
	 * @return
	 */
	public static byte[] bitmapToBytes(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}
		ByteArrayOutputStream os = new ByteArrayOutputStream(); // 将Bitmap压缩成PNG编码，质量为100%存储
		try {
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);// 除了PNG还有很多常见格式，如jpeg等。
			return os.toByteArray();

		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			try {
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

	/**
	 * 图片转换成字符串
	 * 
	 * @param icon
	 * @return
	 */
	public static String imgToStr(Bitmap icon) {
		String img = "";
		if (icon != null) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			icon.compress(Bitmap.CompressFormat.PNG, 100, baos);
			byte[] data = baos.toByteArray();
			byte[] input = Base64.encode(data, 0);
			try {
				img = new String(input, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				baos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return img;
	}

	/**
	 * 字符串转化成图片
	 * 
	 * @param str
	 * @return
	 */
	public static Bitmap strToBitmap(String str) {
		Bitmap img = null;
		if (!Utils.isEmpty(str)) {
			byte[] output;
			try {
				output = Base64.decode(str.getBytes("utf-8"), 0);
				img = BitmapFactory.decodeByteArray(output, 0, output.length);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return img;
	}

	/**
	 * 字符串转化成图片
	 * 
	 * @param str
	 * @return
	 */
	public static Drawable strToDrawable(String str) {
		Drawable draw = null;
		if (!Utils.isEmpty(str)) {
			byte[] output;
			try {
				output = Base64.decode(str.getBytes("utf-8"), 0);
				Bitmap bm = BitmapFactory.decodeByteArray(output, 0,
						output.length);
				draw = new BitmapDrawable(bm);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return draw;
	}

	public static Drawable bitmapToDrawable(Bitmap bitmap) {
		Drawable draw = null;
		if (bitmap != null) {
			byte[] output;
			try {
				output = bitmapToBytes(bitmap);
				Bitmap bm = BitmapFactory.decodeByteArray(output, 0,
						output.length);
				draw = new BitmapDrawable(bm);
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
		return draw;
	}

	/**
	 * 获取视频截图
	 * 
	 * @param bm
	 * @return
	 */
//	public static String getVideoThumbnail(String path) {
//		try {
//			// 注意:android2.2以上支持
//			Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path,
//					Thumbnails.MINI_KIND);
//			bitmap.compress(Bitmap.CompressFormat.PNG, 100,
//					new ByteArrayOutputStream());
//			return IconUtils.imgToStr(bitmap);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//		return "";
//	}

	/**
	 * 获取图片小图
	 * 
	 * @param bm
	 * @return
	 */
//	public static String getImageThumbnail(Bitmap bm) {
//		try {
//			Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, 100, 100);
//			return IconUtils.imgToStr(bitmap);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//		return "";
//	}
}
